
let memberId, openId = '', orderType = 0, pType = 1, pNo = 1;
const getToken = require('../../utils/get_token.js');
const feedbackApi = require('../common/showToast.js');

let cookie, token;
const ckToken = async (that) => {//token确认
  let nTime = Math.floor(new Date().getTime() / 1000);
  let oldTime = wx.getStorageSync('ntime') || 0;
  if (nTime < oldTime)//现在的时间小于到期时间
  {
    thou(that);
  } else {
    let tokens = await getToken.getToken();
    thou(that);
  }
}
const thou = (that) => {//token没有失效时执行的代码
  if (pType == 5) {
    failList(that);
  } else {
    getList(that);
  }
}
const getAll = function (that) {//订单类型
  let params = ['shop/pingTuan/huoPinDdAndSqDdCount', {"memberId":memberId,"tuanIncome": orderType}]
  getToken.getData(function (res) {
    let data = res.data;
    that.data.list[0].num = Number(data.needPay) <= 99 ? Number(data.needPay) : 99;
    that.data.list[1].num = Number(data.needPin) <= 99 ? Number(data.needPin) : 99;
    that.data.list[2].num = Number(data.waitShip) <= 99 ? Number(data.waitShip) : 99;
    that.data.list[3].num = Number(data.hadShip) <= 99 ? Number(data.hadShip) : 99;
    that.data.list[4].num = Number(data.failPin) <= 99 ? Number(data.failPin) : 99;
    that.setData({
      list: that.data.list
    })
  }, ...params)
};
const getList = (that) => {
  if (that.data.boolT) {
    that.data.boolT = false;
    that.setData({
      hidden: false
    })
    let params = [ 'shop/order/getOrderMemberViewList', {"tuanIncome":3,"state":pType,"id":memberId,"pageNo":pNo}]
    getToken.getData(function (res) {
      console.log(res.data);
      setTimeout(function () { that.data.boolT = true; }, 300);
      let rows = res.data.rows;
      if (rows.length > 0) {
        for (var i = 0; i < rows.length; i++) {
          rows[i].tid = 1;
          rows[i].intTotalPrice = (rows[i].intTotalPrice / 100).toFixed(2);
          rows[i].intPrice = (rows[i].intPrice / 100).toFixed(2);
          if (rows[i].specialName != '') {
            rows[i].specialName = '规格:' + rows[i].specialName;
          }
          if (rows[i].isGroupBuy == 1) {
            rows[i].isShow = 1;
          } else {
            if (rows[i].lastNum >= 1) {
              rows[i].isShow = 2;
            }
            if (rows[i].status == '已发货' || rows[i].status == '交易成功') {
              rows[i].isShow = 3;
            }
          }
          rows[i].loading = false;
          rows[i].disabled = false;
          that.data.arr.push(rows[i]);
        }
      }
      if (that.data.arr.length > 0) {
        that.data.noShow = true;
      } else {
        that.data.noShow = false;
      }
      that.setData({
        hidden: true,
        noShow: that.data.noShow,
        arr: that.data.arr
      })
    }, ...params)
  }
}
const failList = (that) => {
  if (that.data.boolT) {
    that.data.boolT = false;
    that.setData({
      hidden: false
    })
    let params = ['shop/pingTuan/refundRecords', {"pageNo":pNo,"membId": memberId,"pageSize":10}]
    getToken.getData(function (res) {
      console.log(res.data);
      setTimeout(function () {that.data.boolT = true;}, 300);
      let data = res.data;
      if (data.result.length > 0) {
        for (var i = 0; i < data.result.length; i++) {
          data.result[i].tid = 2;
          data.result[i].intPrice = (data.result[i].intTotalPrice / 100).toFixed(2);
          data.result[i].intTotalPrice = (data.result[i].intActualPrice / 100).toFixed(2);
          data.result[i].buyCount = data.result[i].quantity;
          data.result[i].status = '拼团失败';
          that.data.arr.push(data.result[i]);
        }
      }
      if (that.data.arr.length > 0) {
        that.data.noShow = true;
      } else {
        that.data.noShow = false;
      }
      that.setData({
        hidden: true,
        noShow: that.data.noShow,
        arr: that.data.arr
      })
    }, ...params)
  }
}
Page({
  data: {
    currentTab: 0,
    isShow:0,
    showT:false,
    noShow: true,
    hidden: true,
    boolT: true,
    arr:[],
    list: [{ 'name': '待付款', 'num': 0, "indx": 1, "show": false },
      { 'name': '待成团', 'num': 0, "indx": 2, "show": false },
      { 'name': '待发货', 'num': 0, "indx": 3, "show": false },
      { 'name': '待收货', 'num': 0, "indx": 4, "show": false },
      { 'name': '退款退货', 'num': 0, "indx": 6, "show": false }]
  },
  onLoad: function (options) {
    let that = this;
    pNo = 1;
    openId = wx.getStorageSync('openId') || '';
    memberId = wx.getStorageSync('memberId');
    cookie = wx.getStorageSync('cookie') || '';
    token = wx.getStorageSync('token') || '';
    pType = Number(options.currentTab);
    that.data.list.find((item,ele,arr)=>{
      if (item.indx == pType)
      {
        item.show=true;
      }
    })
    ckToken(that);
  },
  onShow: function() {// 页面出现在前台时执行
    let that = this;
    getAll(that);
  },
  changeC: function (e) {//点击中级
    let that = this;
    let index = e.currentTarget.dataset.id;
    pType = index;
    pNo = 1;
    for (let i = 0; i < that.data.list.length; i++) {
      if (pType == that.data.list[i].indx) {
        that.data.list[i].show = true;
      } else {
        that.data.list[i].show = false;
      }
    }
    that.data.arr = [];
    that.setData({
      list: that.data.list,
      arr: that.data.arr
    })
    getList(that);
  },
  subClick:function(e){//进入详情
    let that = this;
    let goodsname = e.currentTarget.dataset.goodsname;
    let memberordersn = e.currentTarget.dataset.memberordersn;
    let tuanlistid = e.currentTarget.dataset.tuanlistid;
    let god = e.currentTarget.dataset.god;
    let gimg ='https://www.globalhexi.com/'+e.currentTarget.dataset.gimg;
    let ado=e.currentTarget.dataset.addordersn;
    wx.setStorageSync('wimg', gimg);
    if (goodsname != "充值订单") {
      if (that.data.boolT) {
        that.data.boolT = false;
        wx.navigateTo({
          url:`../p_orderD/p_orderD?id=${memberordersn}&tuanId=${tuanlistid}&aordersn=${ado}&god=${god}`,
          success: function () {
            setTimeout(function () {that.data.boolT = true;}, 500)
          }
        })
      }
    }
  },
  goLoginst:function(e){
    let that = this;
    let id = e.currentTarget.dataset.id;
    let gimg ='https://www.globalhexi.com/'+e.currentTarget.dataset.gimg;
    wx.setStorageSync('wimg', gimg);
    if (that.data.boolT) {
      that.data.boolT = false;
      wx.navigateTo({
        url: '../logistics/logistics?id=' + id,
        success: function () {
          setTimeout(function () {
            that.data.boolT = true;
          }, 500)
        }
      })
    }
  },
  pay: function (e) {//去付款
    var that = this;
    var orderSn = e.target.dataset.id;//商品id
    var stype = e.target.dataset.stype;//商品类型
    for (let i = 0; i < that.data.arr.length; i++) {
      if (orderSn == that.data.arr[i].orderSn) {
        that.data.arr[i].loading = true;
        that.data.arr[i].disabled = true;
        that.setData({
          arr: that.data.arr
        })
      }
    }
    if (that.data.boolT) {
      that.data.boolT = false;
      if (stype == "CZDD") {
        var data = '{"openId":"' + openId + '","orderSn":"' + orderSn + '","memberId":"' + memberId + '","isMini":"mini"}';
        var Url = 'shop/pay/payCzdd';
      } else {
        var data = '{"openId":"' + openId + '","orderSn":"' + orderSn + '","memberId":"' + memberId + '","isMini":"mini"}';
        var Url = 'shop/pay/createOrderForHexiSMtoPay';
      }
      let params = [ 'shop', Url, data]
      getToken.pay(function (res) {
        console.log(res.data);
        if (stype == "CZDD") {
          var result = res.data.content;
        } else {
          var result = res.data;
        }
        if (res.data.flag) {
          wx.requestPayment({
            'timeStamp': result.timeStamp,
            'nonceStr': result.nonceStr,
            'package': result.package,
            'signType': result.signType,
            'paySign': result.paySign,
            'success': function (res) {
              that.setData({arr:[]})
              orderF = 1;
              getAll(that);
              getList(that);
            },
            'fail': function (res) {
              console.log(res)
            }
          })
        } else {
          feedbackApi.showToast({ title: res.data.message, mask: false })//调用 showToast
        }
        that.data.boolT = true;
        for (let j = 0; j < that.data.arr.length; j++) {
          if (orderSn == that.data.arr[j].orderSn) {
            that.data.arr[j].loading = false;
            that.data.arr[j].disabled = false;
            that.setData({
              arr: that.data.arr
            })
          }
        }
      }, ...params)
    }
  },
  //	页面滑动到底部
  onReachBottom: function () {
    let that = this;
    pNo++;
    if (pType == 5) {
      failList(that);
    } else {
      getList(that);
    }
  },
  onPullDownRefresh: function () {//下拉刷新
    let that = this;
   
  },
  /**
  * 用户点击右上角分享
  */
  onShareAppMessage: function (res) {
    
  }
})
